//== SAGITTARIUS ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"
#include "sagittarius/session/session.h"
#include "sagittarius/builtins/modules/builtinmod.h"
#include <malloc.h>

sg_session* __sg_global_ses;

sg_session* create_session() {
    sg_session* s = malloc(sizeof(sg_session));
    return s;
}

void init_session(sg_session* s, symtab* st) {
    s->smsh = NULL;
    s->st = st;
    s->llvmu = NULL;
    
    //init_smo_heap(&s->smoheap, &s->smsh);
    s->bt = create_builtin_type_ctx();
    s->bf = create_builtin_fun_ctx(s->bt);
    s->pro = create_builtin_protos(s->bt, s->bf);
    s->ml = sg_create_mod_loader();
    
    session_init_symtable(s, st);
    sg_register_builtin_mods(s);
}

void session_init_symtable(sg_session* s, symtab* st) {
    install_builtin_syms(st, s->bt);
    install_builtin_fun_syms(s->bf, st);
}

void close_session(sg_session* s) {
    //if(s->smsh)
        //free(s->smsh);
    destroy_smo_heap(&s->smoheap);
    release_builtin_type_ctx(s->bt);
    release_builtin_fun_ctx(s->bf);
    sg_release_mod_loader(s->ml);
}

void release_session(sg_session* s) {
    close_session(s);
    free(s);
}

//-- Global --

sg_session* sg_global_session() {
    return __sg_global_ses;
}

void sg_init_global_session(symtab* st) {
    __sg_global_ses = create_session();
    init_session(__sg_global_ses, st);
}

void sg_close_global_session() {
    release_session(__sg_global_ses);
}
